from rest_framework import serializers
from .models import (
    AdSpace, Advertisement, AdClick,
    AdImpression, AdCampaign, AdPricing
)


class AdSpaceSerializer(serializers.ModelSerializer):
    class Meta:
        model = AdSpace
        fields = [
            'id', 'name', 'position', 'description', 'width', 'height',
            'base_price', 'discount_rate', 'is_active', 'max_ads'
        ]


class AdSpaceListSerializer(serializers.ModelSerializer):
    active_ads_count = serializers.SerializerMethodField()
    
    class Meta:
        model = AdSpace
        fields = [
            'id', 'name', 'position', 'width', 'height',
            'base_price', 'is_active', 'active_ads_count'
        ]
    
    def get_active_ads_count(self, obj):
        return obj.advertisements.filter(status='active').count()


class AdvertisementSerializer(serializers.ModelSerializer):
    ad_space_name = serializers.CharField(source='ad_space.name', read_only=True)
    advertiser_email = serializers.CharField(source='advertiser.email', read_only=True)
    
    class Meta:
        model = Advertisement
        fields = [
            'id', 'title', 'ad_space', 'ad_space_name', 'advertiser', 'advertiser_email',
            'image', 'link_url', 'alt_text', 'description', 'start_date', 'end_date',
            'status', 'priority', 'view_count', 'click_count', 'click_through_rate',
            'daily_budget', 'total_budget', 'spent_amount'
        ]
        read_only_fields = ['view_count', 'click_count', 'click_through_rate', 'spent_amount']


class AdvertisementCreateSerializer(serializers.ModelSerializer):
    class Meta:
        model = Advertisement
        fields = [
            'title', 'ad_space', 'image', 'link_url', 'alt_text',
            'description', 'start_date', 'end_date', 'priority',
            'daily_budget', 'total_budget'
        ]


class AdvertisementAdminSerializer(serializers.ModelSerializer):
    ad_space_name = serializers.CharField(source='ad_space.name', read_only=True)
    advertiser_email = serializers.CharField(source='advertiser.email', read_only=True)
    
    class Meta:
        model = Advertisement
        fields = [
            'id', 'title', 'ad_space', 'ad_space_name', 'advertiser', 'advertiser_email',
            'image', 'link_url', 'alt_text', 'description', 'start_date', 'end_date',
            'status', 'priority', 'reviewed_by', 'reviewed_at', 'review_note',
            'view_count', 'click_count', 'click_through_rate',
            'daily_budget', 'total_budget', 'spent_amount'
        ]
        read_only_fields = ['view_count', 'click_count', 'click_through_rate', 'spent_amount']


class AdClickSerializer(serializers.ModelSerializer):
    advertisement_title = serializers.CharField(source='advertisement.title', read_only=True)
    
    class Meta:
        model = AdClick
        fields = [
            'id', 'advertisement', 'advertisement_title', 'user', 'ip_address',
            'user_agent', 'created_at'
        ]
        read_only_fields = ['id', 'created_at']


class AdImpressionSerializer(serializers.ModelSerializer):
    advertisement_title = serializers.CharField(source='advertisement.title', read_only=True)
    
    class Meta:
        model = AdImpression
        fields = [
            'id', 'advertisement', 'advertisement_title', 'user', 'ip_address',
            'user_agent', 'created_at'
        ]
        read_only_fields = ['id', 'created_at']


class AdCampaignSerializer(serializers.ModelSerializer):
    advertiser_email = serializers.CharField(source='advertiser.email', read_only=True)
    
    class Meta:
        model = AdCampaign
        fields = [
            'id', 'name', 'advertiser', 'advertiser_email', 'description',
            'hypothesis', 'start_date', 'end_date', 'status', 'total_budget',
            'daily_budget', 'total_impressions', 'total_clicks', 'total_spent'
        ]
        read_only_fields = ['total_impressions', 'total_clicks', 'total_spent']


class AdCampaignCreateSerializer(serializers.ModelSerializer):
    class Meta:
        model = AdCampaign
        fields = [
            'name', 'description', 'hypothesis', 'start_date', 'end_date',
            'total_budget', 'daily_budget'
        ]


class AdPricingSerializer(serializers.ModelSerializer):
    ad_space_name = serializers.CharField(source='ad_space.name', read_only=True)
    
    class Meta:
        model = AdPricing
        fields = [
            'id', 'ad_space', 'ad_space_name', 'price_type', 'base_price',
            'peak_multiplier', 'off_peak_multiplier', 'valid_from', 'valid_to',
            'is_active'
        ]


class AdStatsSerializer(serializers.Serializer):
    total_impressions = serializers.IntegerField()
    total_clicks = serializers.IntegerField()
    click_through_rate = serializers.FloatField()
    total_spent = serializers.DecimalField(max_digits=10, decimal_places=2)
    cost_per_click = serializers.DecimalField(max_digits=10, decimal_places=2)


class AdSpaceStatsSerializer(serializers.Serializer):
    ad_space_name = serializers.CharField()
    total_impressions = serializers.IntegerField()
    total_clicks = serializers.IntegerField()
    revenue = serializers.DecimalField(max_digits=10, decimal_places=2)
    active_ads = serializers.IntegerField()


class CampaignPerformanceSerializer(serializers.Serializer):
    campaign_name = serializers.CharField()
    impressions = serializers.IntegerField()
    clicks = serializers.IntegerField()
    ctr = serializers.FloatField()
    spent = serializers.DecimalField(max_digits=10, decimal_places=2)
    cpc = serializers.DecimalField(max_digits=10, decimal_places=2)


class AdvertisementFilterSerializer(serializers.Serializer):
    ad_space = serializers.IntegerField(required=False)
    status = serializers.ChoiceField(
        choices=['pending', 'active', 'paused', 'expired', 'rejected'],
        required=False
    )
    start_date = serializers.DateField(required=False)
    end_date = serializers.DateField(required=False)
    advertiser = serializers.IntegerField(required=False)


class AdPerformanceReportSerializer(serializers.Serializer):
    date = serializers.DateField()
    impressions = serializers.IntegerField()
    clicks = serializers.IntegerField()
    ctr = serializers.FloatField()
    revenue = serializers.DecimalField(max_digits=10, decimal_places=2)
